package com.demo.bookstore.controller;

import com.demo.bookstore.model.BookEntity;
import com.demo.bookstore.model.base.Response;
import com.demo.bookstore.service.BookService;
import com.demo.bookstore.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@Slf4j
public class SearchBookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/book/query")
    public Response<List<BookEntity>> query(@RequestParam("name") String name) {
        List<BookEntity> ret = bookService.queryBooks(name);
        return ResponseUtil.ok(ret);
    }

    @PostMapping("/book/add")
    public Response<Boolean> add(@RequestBody BookEntity book) {
        Boolean ret = bookService.addBook(book);
        return ResponseUtil.ok(ret);
    }
    @GetMapping("/book/delete")
    public Response<Boolean> deleteById(@RequestParam("id") int id){
        return ResponseUtil.ok(bookService.deleteById(id));
    }

    @GetMapping("/book/queryAllByPage")
    public Response<List<BookEntity>> queryAllByPage(@RequestParam("pageBegin") int pageBegin, @RequestParam("pageSize") int pageSize){
        return ResponseUtil.ok(bookService.queryAllByPage(pageBegin,pageSize));
    }

    @PostMapping("book/batchInsert")
    public Response<Integer> batchInsertBook(@RequestBody List<BookEntity> bookEntityList) {
        return ResponseUtil.ok(bookService.batchInsert(bookEntityList));
    }

    @PostMapping("/book/update")
    public Response<Boolean> update(@RequestBody BookEntity book) {
        return ResponseUtil.ok(bookService.updateBook(book));
    }

    @PostMapping("/book/export")
    public void export(@RequestBody BookEntity book, HttpServletResponse response) {
        String content = bookService.export(book);
        if (null == content) {
            content = "";
        }
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/plain");
        response.setHeader("Content-Disposition", "attachment;filename=books.txt");
        response.setHeader("requestType", "file");
        response.setHeader("fileName", "books.txt");
        BufferedOutputStream bufferedOS = null;
        ServletOutputStream servletOS = null;
        try {
            servletOS = response.getOutputStream();
            bufferedOS = new BufferedOutputStream(servletOS);
            bufferedOS.write(content.getBytes(StandardCharsets.UTF_8));
            bufferedOS.flush();
            bufferedOS.close();
        } catch (Exception e) {
            log.error("下载文件失败", e);
        } finally {
            close(bufferedOS);
            close(servletOS);
        }
    }
    private void close(Closeable closeable) {
        if (null == closeable) {
            return;
        }
        try {
            closeable.close();
        } catch (Exception e) {
        }
    }
}
